<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'equipment:alarm:process:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.enterpriseCode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsEquipmentAlarmProcessEquipmentFireAlarmProcessList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { equipmentFireAlarmProcessDelete, equipmentFireAlarmProcessListData } from '/@/api/equipment.alarm.process/equipmentFireAlarmProcess';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { officeTreeData } from '/@/api/sys/office';

  const { t } = useI18n('equipment.alarm.process.equipmentFireAlarmProcess');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('设备上报火情流程表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t('企业名称'),
        field: 'enterpriseName',
        component: 'TreeSelect',
        componentProps: {
          api: officeTreeData,
          fieldNames: { label: 'name', value: 'name' }
        }
      },
      {
        label: t('设备名称'),
        field: 'equipmentName',
        component: 'Input',
      },
      {
        label: t('监督员姓名'),
        field: 'supervisorName',
        component: 'Input',
      },
      {
        label: t('预警时间'),
        field: 'alertTime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      
      {
        label: t('超时情况'),
        field: 'hasTimeout',
        component: 'Select',
        componentProps: {
          options: [
            { label: t('超时'), value: '1' },
            { label: t('未超时'), value: '2' },
          ]
        }
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('企业名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备ID'),
      dataIndex: 'equipmentId',
      key: 'a.equipment_id',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备名称'),
      dataIndex: 'equipmentName',
      key: 'a.equipment_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('监督员姓名'),
      dataIndex: 'supervisorName',
      key: 'a.supervisor_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('预警时间'),
      dataIndex: 'alertTime',
      key: 'a.alert_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('预警名称'),
      dataIndex: 'alertName',
      key: 'a.alert_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('预警内容，详细描述预警信息'),
    //   dataIndex: 'alertContent',
    //   key: 'a.alert_content',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('详细地址'),
      dataIndex: 'detailedAddress',
      key: 'a.detailed_address',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否有火情'),
      dataIndex: 'hasFire',
      key: 'a.has_fire',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('超时情况'),
      dataIndex: 'hasTimeout',
      key: 'a.has_timeout',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑设备上报火情流程表'),
        onClick: handleForm.bind(this, { tid: record.tid }),
        auth: 'equipment:alarm:process:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除设备上报火情流程表'),
        popConfirm: {
          title: t('是否确认删除设备上报火情流程表'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'equipment:alarm:process:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'i-fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerTable, { reload }] = useTable({
    api: equipmentFireAlarmProcessListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await equipmentFireAlarmProcessDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'equipment_fire_warning_process', bizKey: record.id });
  }
</script>
